package com.itheima.shengxiao2;

public class Ganfanren implements Runnable {
  private   Desk desk;

    public Ganfanren(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true){
            synchronized (desk.getObj()) {
                String name = Thread.currentThread().getName();
                //看看有没有饭
                if(desk.isFan()){
                    //有饭 干饭  摇人 让继续做饭
                    System.out.println(name+"干饭咯");
                    desk.setFan(false);
                    desk.setCount(desk.getCount()-1);
                    desk.getObj().notifyAll();
                }else {
                    //没有  等着
                    try {
                        desk.getObj().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(desk.getCount()==0){
                    break;
                }
            }


        }

    }
}
